#include "ecb/base/RExporter.h"

#include "ecb/base/Problem.h"
#include "ecb/base/Properties.h"
#include "ecb/base/Math.h"

#include <sstream>
namespace ecb {

    std::string RExporter::LandscapeScript(Problem* function) {
        std::string id = function->properties()->getId();
        scalar minimum = function->properties()->getMinimum();
        scalar maximum = function->properties()->getMaximum();


        std::stringstream ss;
        ss << "cols = colorRampPalette(c('black','white'));\n";
        std::vector<scalar> x_y;
        int samples = 100;
        for (int i = 1; i <= samples; ++i) {
            x_y.push_back(Math::Scale<scalar > (1.0, samples, i, minimum, maximum));
        }



        ss << id + "_x = " + id + "_y = " + Math::C(x_y) + ";\n";

        std::vector<scalar> z;
        for (size_t i = 0; i < x_y.size(); ++i) {
            for (size_t j = 0; j < x_y.size(); ++j) {
                std::vector<scalar> input;
                input.push_back(x_y[i]);
                input.push_back(x_y[j]);
                z.push_back(function->f(input));
            }
        }

        ss << id << "_z = matrix(" << Math::C(z) << ", " << x_y.size() << ", " << x_y.size() << ", "
                << "byrow=TRUE);\n\n";
        ss << "image(" << id << "_x, " << id << "_y, " << id << "_z,"
                << "col=cols(255),axes=FALSE,xlab=NA,ylab=NA,asp=1,main='" << id << "');\n";

        return ss.str();
    }
}
